import { createApp } from 'vue'
import { createPinia } from 'pinia'
import piniaPluginPersistedstate from 'pinia-plugin-persistedstate'
import router from './router/index'
import App from './App.vue'
import ElementPlus from 'element-plus'
import 'element-plus/dist/index.css'
import '@/assest/style/login.css'
import "nprogress/nprogress.css"
import * as ElementPlusIconsVue from '@element-plus/icons-vue'
import  useMenuStore  from '@/store/menulist.js'
import useUserStore  from '@/store/user.js'
import zhCn from 'element-plus/dist/locale/zh-cn.mjs'
let app = createApp(App)
for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
  app.component(key, component)
}
let pinia = createPinia()
app.use(pinia)
pinia.use(piniaPluginPersistedstate)

router.beforeEach(async (to,from) => {
  let token =  sessionStorage.getItem('token')
  if(!token){
    if(to.path !== '/login'){
        return{
           path:'/login',
        }
      }
  }else{
    let menuStore = useMenuStore()
    if(menuStore.menulist.length<1){
      await menuStore.getMenulist()
      // return{path:to.path,query:to.query,params:to.params}
      await router.replace(to.path)
    }
   
    
  }
})
let userStore = useUserStore()
router.afterEach((to,from)=>{
  // console.log(to);
  // console.log(from);
  if(to.meta.key){
    userStore.breadCrumbList =to.matched.filter(item=>{
      return item.path !=='/'
    })
  }
  // console.log(userStore.breadCrumbList);
})
app.use(ElementPlus)
app.use(ElementPlus, {
  locale: zhCn,
})
app.use(router)

app.mount('#app')
